package com.etc.my.controller;


import com.etc.my.dto.CollectionAndCarMessageDto;
import com.etc.my.dto.CollectionDto;
import com.etc.my.dto.MyMessage;
import com.etc.my.dto.PageDto;
import com.etc.my.entity.User;
import com.etc.my.service.MyCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class MyCollectionController {
    @Autowired
   private MyCollectionService mcs;

    public MyCollectionService getMcs() {
        return mcs;
    }

    public void setMcs(MyCollectionService mcs) {
        this.mcs = mcs;
    }


    @RequestMapping("/getCollectionByUserId")
    @ResponseBody
    public MyMessage getCollectionByUserId(HttpSession session, Integer page, Integer size) {
        MyMessage message = new MyMessage();
        //获取登录的用户
      User user = (User) session.getAttribute("loginuser");


        if (user != null) {
            //判断是否有取到值
            if (page == null || page <= 0) {
                page = 1;
            }

            if (size == null || size <= 0) {
                size = 3;
            }
            //获取总条数
            Integer allCount = mcs.getCollectionCountByUserId(user.getUser_id()).getAllCount();
            if (allCount >= 0 && allCount != null) {
                Integer maxPage = (allCount % size == 0) ? (allCount / size) : (allCount / size + 1);
                //页数条数上限容错
                page = page > maxPage ? maxPage : page;
                size = size > 3 ? 3 : size;
                //页数条数下限容错
                page = page <= 0 ? 1 : page;
                size = size <= 0 ? 3 : size;

                PageDto pd = new PageDto();
                pd.setPage(page);
                pd.setSize(size);

                //调用方法查询
                List<CollectionAndCarMessageDto> pushList =mcs.getCollectionByUserIdOfPage(user.getUser_id(), (page - 1) * size, size);
                if (pushList.size() > 0 && pushList != null) {
                    message.setFlag(1);
                    pd.setMaxPage(maxPage);
                    pd.setAllCount(allCount);
                    pd.setOlist(pushList);
                    message.setData(pd);
                } else {
                    message.setFlag(0);
                    message.setInfo("查无此纪录");
                }
            } else {
                message.setFlag(0);
                message.setInfo("查无此纪录");
            }

        } else {
            message.setFlag(0);
            message.setInfo("您尚未登录请先登录!");
        }

        return  message;
    }

    @RequestMapping("/cancelCollectionById")
    public void cancelCollectionById(HttpServletResponse response,Integer collection_id) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(collection_id!=null&&collection_id>0){
            if(mcs.cancelCollectionById(collection_id)){
                out.print("<script>alert('取消收藏');location.href='mycollection.jsp'</script>");
            }else{
                out.print("<script>alert('取消收藏失败，请重新操作!');history.go(-1)</script>");
            }
        }else{
            out.print("<script>alert('参数有误取消收藏失败，请重新操作!');location.href='mycollection.jsp'</script>");
        }
        out.flush();
        out.close();
    }

    @RequestMapping("/changeCollection")
    @ResponseBody
    public MyMessage changeCollection(Integer user_id,String car_id,Integer flag){
        MyMessage message = new MyMessage();
        if(user_id!=null && car_id!=null && flag!=null){
            if(flag==1){//收藏
                //判断是否收藏成功
                if(mcs.addCollection(user_id,car_id)){
                    message.setFlag(1);
                }else{
                    message.setFlag(0);
                    message.setInfo("收藏失败，请稍后再试...");
                }
            }else if(flag==2){//取消收藏
                //判断取消收藏是否成功
                if(mcs.cancelCollectionByCarAndUserId(user_id,car_id)){
                    message.setFlag(1);
                }else{
                    message.setFlag(0);
                    message.setInfo("取消收藏失败，请稍后再试...");
                }
            }
        }else{
            message.setFlag(0);
            message.setInfo("用户或者车辆参数有误！");
        }
        return message;
    }
}
